library(here)
library(tidyverse)

load(here("Analysis", "analysis_sameDAviewDA.rda"), verbose = TRUE) ## wrkdat_DA are people who were shown their DA

pdf(file = here("Figures_Tables", "coefplot_SameDAViewDA.pdf"), width = 8, height = 2)
par(oma = rep(0, 4), mar = c(4, 1, 2, 1), mgp = c(3, .5, 0), xpd = NA)
oldmai <- par("mai")
newmai <- oldmai
newmai[2] <- max(strwidth(row.names(res1SameDAViewDA[1:2, ]), units = "inches")) + .1
par(mai = newmai)
nmods <- nrow(res1SameDAViewDA[1:2, ])
theylim <- range(c(0, as.vector(res1SameDAViewDA[1:2, ])[is.finite(as.vector(res1SameDAViewDA[1:2, ]))]))
plot(theylim, c(1, nmods),
  type = "n", axes = FALSE, ylab = "",
  xlab = "Mean Difference Between High and Low Perceivers of VM in DA \n (+/- 75% Coverage Interval)"
)
segments(res1SameDAViewDA[1:2, "lo"], 1:nmods, res1SameDAViewDA[1:2, "hi"], 1:nmods)
points(res1SameDAViewDA[1:2, "mean"], 1:nmods, pch = 19)
abline(v = 0, lwd = .5, col = gray(.7))
axis(1, line = .5)
axis(2, at = 1:nmods, labels = row.names(res1SameDAViewDA[1:2, ]), las = 1)
par(mai = oldmai)
dev.off()

## Trying to show more data in this next plot

da_level_dat <- sameDAviewDAdat %>%
  group_by(dauid) %>%
  summarize(
    "Social cohesion" = social.capital01[perc_more == 1] - social.capital01[perc_more == 0],
    "Collective efficacy" = community.resp01[perc_more == 1] - community.resp01[perc_more == 0]
  )

da_long <- pivot_longer(data = da_level_dat, cols = c("Social cohesion", "Collective efficacy"))

g_sc <- ggplot(data = da_long, aes(x = name, y = value)) +
  geom_boxplot() +
  geom_jitter(width = 0) +
  geom_hline(yintercept = 0) +
  stat_summary(fun = mean, color = "black", shape = "triangle") +
  ylab("") +
  xlab("") +
  theme_minimal()
g_sc

ggsave(g_sc, file = here("Figures_Tables", "pair_diffs_sameDAviewDA.pdf"), width = 6, height = 6)


system("touch Figures_Tables/sameDAViewDA_figures.done")
